from numpy import *

def loadDataSet(filename):
    dataMat = []; labelMat = []
    fr = open(filename)
    for line in fr.readlines():
        lineArr = line.strip().split('\t')
        dataMat.append([float(lineArr[0]), float(lineArr[1])])
        labelMat.append(float(lineArr[2]))
    return dataMat, labelMat

def selectJrand(i,m):
    """
     
    :param i:
    :param m:
    :return: 找一个 j！= i的 j，并返回
    """
    j = i
    while(j==i):
        j = int(random.uniform(0, m))
    return j
def clipAlpha(aj, H, L):
    """

    :param aj: alpha
    :param H:  上限
    :param L: 下限
    :return:
    """
    if aj > H:
        aj = H
    if L > aj:
        aj = L
    return aj
#简化版smo算法
def smoSimple(dataMatIn, classLabels, C, tolar, maxIter):
    """

    :param dataMatIn: 数据集
    :param classLabels: 类别标签
    :param C: 常数C
    :param tolar: 容错率
    :param matIter: 退出前最大的循环次数
    :return:
    """
    dataMatrix = mat(dataMatIn); labelMat = mat(classLabels).transpose()
    b = 0; m, n = shape(dataMatrix)
    alphas = mat(zeros((m, 1)))
    iter = 0
    while(iter < maxIter):
        alphaPairsChanged = 0
        for i in range(m):
            fXi = float(multiply(alphas, labelMat).T*\
                    (dataMatrix*dataMatrix[i,:].T)) + b
            print ("fXi:", fXi)
            Ei = fXi - float(labelMat[i])
            if ((labelMat[i] * Ei < -tolar) and (alphas[i] < C)) or\
                    ((labelMat[i] * Ei > tolar) and(alphas[i] > 0)):
                j = selectJrand(i, m)
                fXj = float(multiply(alphas, labelMat).T*\
                            (dataMatrix*dataMatrix[j,:].T)) + b
                Ej = fXj - float(labelMat[j])
                alphaIold = alphas[i].copy()
                alphaJold = alphas[j].copy()
                if(labelMat[i] != labelMat[j]):
                    L = max(0, alphas[j] - alphas[i])
                    H = min(C, C + alphas[j] - alphas[i])
                else:
                    L = max(0, alphas[j] + alphas[i] - C)
                    H = min(C, C + alphas[j]+alphas[i])
                if L == H: print("L == H");continue
                eta = 2.0 * dataMatrix[i, :]*dataMatrix[j,:].T - \
                    dataMatrix[i, :]*dataMatrix[i,:].T - \
                    dataMatrix[j, :]*dataMatrix[j, :].T
                if eta >= 0: print("eta >= 0"); continue
                alphas[j] -= labelMat[j]*(Ei - Ej) / eta
                alphas[j] = clipAlpha(alphas[j], H, L)
                if(abs(alphas[j] - alphaJold) < 0.00001): print\
                    ("j not moving enough"); continue
                alphas[i] += labelMat[j] * labelMat[i]*\
                             (alphaJold - alphas[j])
                b1 = b - Ei - labelMat[i] * (alphas[i]-alphaIold) *\
                    dataMatrix[i, :] * dataMatrix[i,:].T - \
                    labelMat[j] * (alphas[j]-alphaJold)*\
                    dataMatrix[i,:]*dataMatrix[j, :].T
                b2 = b - Ej - labelMat[i] * (alphas[i] - alphaIold)*\
                    dataMatrix[i,:]*dataMatrix[j, :].T - \
                    labelMat[j]*(alphas[j] - alphaJold) * \
                    dataMatrix[j,:]*dataMatrix[j, :].T
                if(0 < alphas[i]) and (C > alphas[i]): b = b1
                elif (0 < alphas[j]) and (C > alphas[j]): b = b2
                else: b = (b1 + b2) / 2.0
                alphaPairsChanged += 1
                #print("iter: %d i: %d, pairs changed %d" %\
                #      (iter, i, alphaPairsChanged))
        if(alphaPairsChanged == 0): iter += 1
        #else: iter = 0
        #print("iteration number: %d" % iter)
    return b, alphas
dataMat, labelMat = loadDataSet('testSet.txt')
#print(labelMat)
#b, alphas = smoSimple(dataMat, labelMat, 0.6, 0.001, 4)
#print(alphas)
#print(b)
m,n = shape(dataMat)
alphas = mat(zeros((m, 1)))
maps = multiply(alphas, labelMat)
m, n = shape(maps)
print(m,n)
#print(alphas)
dataMat = mat(dataMat)
labelMat = mat(labelMat)
#print(dataMat[2,:])
#print(dataMat)
#fX1 = float(multiply(alphas, labelMat).T)
#print(fX1)